<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- tabs 标签页 -->
        <el-tabs @tab-click="handleClick">
          <el-tab-pane label="角色管理">
            <el-button type="primary" size="medium" @click="hAdd">+ 新增角色</el-button>
            <!-- 表格 -->
            <el-table
              :data="tableData"
              style="width: 100%"
            >
              <el-table-column
                label="序号"
                type="index"
                :index="indexMethod"
              />
              <el-table-column
                prop="name"
                label="角色名称"
                width="180"
              />
              <el-table-column
                prop="description"
                label="描述"
              />
              <el-table-column label="操作">
                <template slot-scope="scope">
                  <el-button size="mini" type="success">分配权限</el-button>
                  <el-button size="mini" @click="hEdit(scope.row)">编辑</el-button>
                  <el-button size="mini" type="danger" @click="handleDelete(scope)">删除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-tab-pane>
          <!-- 分页 -->
          <el-pagination
            background
            :current-page="paging.page"
            :page-sizes="[1, 2, 3, 5]"
            layout="total, sizes, prev, pager, next, jumper"
            :total="total"
            :page-size="paging.pagesize"
            @size-change="handleSizeChange"
            @current-change="handleCurrentChange"
          />
        </el-tabs>
      </el-card>
      <!-- 弹出层 -->
      <el-dialog
        title="编辑或增加"
        :close-on-click-modal="false"
        :close-on-press-escape="false"
        :visible.sync="isDialog"
        @close="closeDialog"
      >
        <el-form :model="form" :rules="rules">
          <el-form-item label="角色名称" prop="name" label-width="100px">
            <el-input v-model="form.name" style="width:80%" placeholder="请输入内容" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description" label-width="100px">
            <el-input v-model="form.description" style="width:80%" placeholder="请输入内容" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="isDialog = false">取 消</el-button>
          <el-button type="primary" @click="hSubmit">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>

</template>

<script>
import { getRole, delRole, addRole, editRole } from '@/api/settings'
export default {
  name: '',
  components: {},
  props: {},
  data() {
    return {
      // 表格
      tableData: [],
      // 分页
      total: 0,
      paging: {
        page: 1, // 当前 页
        pagesize: 2 // 当前 一页几条
      },
      // 判断是增加还是编辑
      isEdit: false,
      // 弹出框
      isDialog: false,
      form: {
        name: '',
        description: ''
      },
      // 弹出框 表单校验
      rules: {
        name: [
          { required: true, message: '请输入角色名称', trigger: 'blur' }
        ],
        description: [
          { message: '请输入角色描述', trigger: 'blur' }]
      }
    }
  },
  computed: {},
  watch: {},
  created() {
    this.getRoleApi()
  },
  mounted() {},
  methods: {
    // 获取当前页角色列表
    async getRoleApi() {
      try {
        const res = await getRole(this.paging)
        this.tableData = res.data.rows
        console.log(this.tableData)
        this.total = res.data.total
      } catch (err) {
        console.log(err)
      }
    },
    // 标签页
    handleClick(tab, event) {
      console.log(tab, event)
    },
    // 分页
    // 当前 一页几条
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`)
      this.paging.pagesize = val
      this.getRoleApi()
    },
    // 当前页
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`)
      this.paging.page = val
      this.getRoleApi()
    },
    // 删除角色
    handleDelete(scope) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        if (this.tableData.length === 1) {
          this.paging.page--
          if (this.paging.page < 1) {
            this.paging.page = 1
          }
        }
        await delRole(scope.row.id)
        await this.getRoleApi()
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    },
    // 表格序号
    indexMethod(index) {
      // this.paging.page 当前页码
      // this.paging.pagesize 当前每一页数据条数
      return index + 1 + (this.paging.page - 1) * (this.paging.pagesize)
    },
    // 新增角色
    hAdd() {
      // 进入新增
      this.isEdit = false
      this.isDialog = true
    },
    async doAdd() {
      try {
        // 1 发送添加请求
        const res = await addRole(this.form)
        // 2 提示成功
        this.$message({
          message: '推荐成功',
          type: 'success'
        })
        // 3 计算添加后最后一页是第几页
        this.total++
        this.paging.page = Math.ceil(this.total / this.paging.pagesize)
        // 4 重新渲染数据 跳转页面到最新添加页面
        this.getRoleApi()
        console.log('添加角色成功', res)
      } catch (err) {
        console.log('添加角色失败', err)
      }
    },
    // 编辑角色
    hEdit(row) {
      // 1 进入编辑
      this.isEdit = true
      // 2 弹出框
      this.isDialog = true
      // 3 表单数据回填
      this.form = { ...row }
    },
    async doEdit() {
      try {
        // 1.发送请求
        const res = await editRole(this.form)
        // 2.提示修改成功消息
        this.$message({
          message: '修改成功',
          type: 'success'
        })
        // 3.重新渲染页面
        this.getRoleApi()
        console.log('修改成功', res)
      } catch (err) {
        console.log('修改失败', err)
      }
    },
    // 弹出框确认提交
    hSubmit() {
      this.isEdit ? this.doEdit() : this.doAdd()
      //  关闭弹出框
      this.isDialog = false
    },
    // 弹出层关闭时的回调
    closeDialog() {
      this.form = {
        name: '',
        description: ''
      }
    }
  }
}
</script>

<style scoped >
.el-pagination{
  text-align: center;
  margin-top: 20px;
}
</style>

